package com.sdp.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.service.RightExtService;
import com.sdp.right.utils.RightConstants;
import com.sdp.wx.cache.clean.WxCleanCache;
import com.sdp.wx.db.wx_emp.entity.WxEmp;
import com.sdp.wx.db.wx_emp.service.IWxEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/WxEmpController")
public class WxEmpController {

	@Autowired
	private IWxEmpService iWxEmpService;

	@Autowired
	private UniqueCheckTools uniqueCheckTools;

	@Autowired
	private Query query;

	@Autowired
	private RightExtService rightExtService;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private WxCleanCache wxCleanCache;



	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxEmpService, WxEmp.class, dataCenter));
	}


	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxEmp emp) throws NoSuchAlgorithmException {
		if(emp==null) {
			return Result.error("人员信息为空");
		}
		if(StringUtils.isEmpty(emp.getKeyId())) {
			//账号不能重复和工号不能重复
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxEmpService, WxEmp.class, new String[] {"empNo","accountNo"}, "keyId", emp, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("账号或者工号重复");
			}
			emp.setCreateTime(LocalDateTime.now());
			emp.setIsUse(RightConstants.是.getCode());
			SysEmp sysEmp = new SysEmp();
			sysEmp.setEmpName(emp.getEmpName());
			sysEmp.setEmpNo(emp.getEmpNo());
			sysEmp.setAccountNo(emp.getAccountNo());
			sysEmp.setOrgNo(emp.getOrgNo());
			sysEmp.setCreateTime(LocalDateTime.now());
			sysEmp.setEmpPhone(emp.getEmpPhone());
			sysEmp.setIsUse(RightConstants.是.getCode());
			sysEmp.setProfilePicture(emp.getProfilePicture());
			rightExtService.addSysEmp(sysEmp);
			iWxEmpService.save(emp);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxEmpService, WxEmp.class, new String[] {"empNo","accountNo"}, "keyId", null, emp, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("账号或者工号重复");
			}
			SysEmp sysEmp = new SysEmp();
			sysEmp.setEmpName(emp.getEmpName());
			sysEmp.setOrgNo(emp.getOrgNo());
			sysEmp.setEmpPhone(emp.getEmpPhone());
			sysEmp.setProfilePicture(emp.getProfilePicture());
			rightExtService.updateSysEmpByAccountNo(emp.getAccountNo(), sysEmp);
			iWxEmpService.updateById(emp);
		}
		redisUtil.del(WxEmp.class, emp.getEmpNo());
		wxCleanCache.cleanEmpCache();
		wxCleanCache.cleanEmp();
		return Result.ok();
	}


	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="accountNo",required=true) String accountNo,@RequestParam(name="empNo",required=true) String empNo) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		//删除人员
		iWxEmpService.removeById(keyId);
		rightExtService.removeSysEmpByaccountNo(accountNo);
		redisUtil.del(WxEmp.class, empNo);
		wxCleanCache.cleanEmpCache();
		return Result.ok();
	}
	/**
	 * 职工详细信息
	 * @param empName
	 * @return
	 */
	@GetMapping(value = "/seachWxEmp")
	public Result<?> seachWxEmp(@RequestParam(name = "empName",required = true) String empName) {
		QueryWrapper<WxEmp> queryWrapper=new QueryWrapper<>();
		queryWrapper.lambda().like(WxEmp::getEmpName,empName);
		List<WxEmp> wxEmpList=iWxEmpService.list(queryWrapper);
		return Result.ok(wxEmpList);
	}
}

